#include "PreCompile.hpp"
#include "PkgConfig.hpp"

namespace BuildNG
{
  Glib::RefPtr<PkgConfig> PkgConfig::get_pkg_config(const Glib::ustring &pkg_name)
  {
    return Glib::RefPtr<PkgConfig>(new PkgConfig(pkg_name));
  }
  
  PkgConfig::PkgConfig(const Glib::ustring &pkg_name)
    : pkg_name(pkg_name)
  {
    std::string stderr_res;
    int exit_status;
    
    std::string cflags_stdstr;
    Glib::spawn_command_line_sync("pkg-config --cflags " + pkg_name,
      &cflags_stdstr,&stderr_res,&exit_status);
    if(exit_status)
    {
      throw PkgConfigException("Error while running pkg-config: " + stderr_res);
    }
    cflags = cflags_stdstr;
    
    std::string libs_stdstr;
    Glib::spawn_command_line_sync("pkg-config --libs " + pkg_name,
      &libs_stdstr,&stderr_res,&exit_status);
    if(exit_status)
    {
      throw PkgConfigException("Error while running pkg-config: " + stderr_res);
    }
    lib_flags = libs_stdstr;
    
    log("Read pkg-config data for " + pkg_name + ": ");
    log("\tcflags: " + cflags);
    log("\tlibs: " + lib_flags);
  }
}
